package com.colortea.action.frontstage.membercenter;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.colortea.action.backstage.base.BaseTAction;
import com.colortea.action.backstage.base.DataCollectionTAction;
import com.colortea.action.backstage.utils.BaseTools;
import com.colortea.action.backstage.utils.enums.BaseEnums.OrderReturnStatus;
import com.colortea.action.backstage.utils.statickey.StaticKey;
import com.colortea.action.frontstage.vo.OrderReturnGoodsTVo;
import com.colortea.entity.MemberT;
import com.colortea.entity.OrderReturnGoodsT;
import com.colortea.service.OrderReturnGoodsTService;
import com.opensymphony.xwork2.ActionContext;

@ParentPackage("colortea")
@Namespace("/mcenter")
@InterceptorRefs({ @InterceptorRef("defaultStack") })
public class ReturnGoodsAction extends BaseTAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3302366901085960455L;

	@Resource
	private DataCollectionTAction dataCollectionTAction;
	@Resource
	private OrderReturnGoodsTService orderReturnGoodsTService;

//	@Action(value = "savedeliveraddress", results = { @Result(name = "json", type = "json") })
//	public String saveDeliverAddress() {
//		MemberT member=(MemberT)ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
//		if(member!=null){
//			this.setSlogin(true);
//	
//			this.setSucflag(true);
//			return JSON;
//		}
//		this.setSlogin(false);		
//		return JSON;
//	}
	/**
	 * 进入售后服务界面
	 * @return
	 */
	@Action(value = "goreturngoods", results = { 
			@Result(name = "success",type="freemarker",location = "/WEB-INF/theme/default/shop/returngoods.ftl"),
			@Result(name = "gologin",type="freemarker",location = "/WEB-INF/theme/default/shop/login.ftl"),
	})
	public String goreturngoods(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){
			Criterion criterion = Restrictions.and(Restrictions.eq("memberid",memberT.getId()));
			List<OrderReturnGoodsT> list = this.orderReturnGoodsTService.findByCriteria(OrderReturnGoodsT.class, criterion);
			if(list.size()>0){
				List<OrderReturnGoodsTVo> listVo = new ArrayList<OrderReturnGoodsTVo>();
				for (OrderReturnGoodsT orderReturnGoodsT : list) {
					OrderReturnGoodsTVo goodsTVo = new OrderReturnGoodsTVo();
					goodsTVo.setId(orderReturnGoodsT.getId());
					goodsTVo.setCreatetime(orderReturnGoodsT.getCreatetime());
					goodsTVo.setImgs(orderReturnGoodsT.getImgs());
					goodsTVo.setOrderid(orderReturnGoodsT.getOrderid());
					goodsTVo.setStatus(OrderReturnStatus.getName(orderReturnGoodsT.getStatus()));
					goodsTVo.setTitle(orderReturnGoodsT.getTitle());
					listVo.add(goodsTVo);
				}
				ActionContext.getContext().getSession().put("goodsreturnlist", listVo);
			}			
			return SUCCESS;
		}
		return "gologin";		
	}
	/**
	 * 进入售后服务界面详情
	 * @return
	 */
	@Action(value = "goreturngoodsdetailbyId",results = { 	
			@Result(name = "success",type="freemarker",location = "/WEB-INF/theme/default/shop/returngoodsdetail.ftl"),
			@Result(name = "gologin",type="freemarker",location = "/WEB-INF/theme/default/shop/login.ftl"),
			@Result(name = "goreturngoods",type = "redirect", location = "/mcenter/goreturngoods.action")})
	public String goreturngoodsdetailbyId(){
		dataCollectionTAction.putBaseInfoToContext();
		MemberT memberT=(MemberT) ActionContext.getContext().getSession().get(StaticKey.MEMBER_SESSION_KEY);
		if(memberT!=null){	
			this.setSlogin(true);
			if (StringUtils.isNotBlank(this.getId())) {
				OrderReturnGoodsT orderReturnGoodsT = this.orderReturnGoodsTService.findByPK(OrderReturnGoodsT.class, this.getId());
				if(orderReturnGoodsT != null){
					OrderReturnGoodsTVo goodsTVo = new OrderReturnGoodsTVo();
					goodsTVo.setImgs(orderReturnGoodsT.getImgs());
					goodsTVo.setOrderid(orderReturnGoodsT.getOrderid());
					goodsTVo.setCreatetime(orderReturnGoodsT.getCreatetime());
					goodsTVo.setStatus(OrderReturnStatus.getName(orderReturnGoodsT.getStatus()));
					goodsTVo.setTitle(orderReturnGoodsT.getTitle());
					ActionContext.getContext().getSession().put("orderReturnGoodsT", goodsTVo);
					this.setSucflag(true);
					return SUCCESS;
				}else{					
					return "goreturngoods";
				}
			}			
			return SUCCESS;
		}
		this.setSlogin(false);
		return "gologin";		
	}
	
	
	
	
	
	
	
	private String id;
	private boolean sucflag;
	private boolean slogin;

	public boolean isSucflag() {
		return sucflag;
	}

	public void setSucflag(boolean sucflag) {
		this.sucflag = sucflag;
	}
	public boolean isSlogin() {
		return slogin;
	}
	public void setSlogin(boolean slogin) {
		this.slogin = slogin;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
}
